import * as cc from "cc";
import VCMsgBox, { MsgBoxType } from "../vc/VCMsgBox";
import { ViewBase } from './../../../../framework/view/ViewBase';

const { ccclass, property, menu } = cc._decorator;
@ccclass("ViewMsgBox")
@menu("GameView/ViewMsgBox")
export default class ViewMsgBox extends ViewBase{

    protected static prefabUrl:string = "game/prefab/mvc/common/MsgBox";
    public static className:string = "ViewMsgBox"
    protected vc:VCMsgBox = null;

    @property({ type: cc.Node, tooltip: "中按钮" })
    btnMiddle: cc.Node = null;
    @property({ type: cc.Node, tooltip: "左按钮" })
    btnLeft: cc.Node = null;
    @property({ type: cc.Node, tooltip: "右按钮" })
    btnRight: cc.Node = null;
    @property({ type: cc.RichText, tooltip: "文本内容" })
    txtMsg: cc.RichText = null;
    @property({ type: cc.Label, tooltip: "标题" })
    txtTitle: cc.Label = null;
    //生命周期：onInit(加载时只调一次)==>onOpen(每次showui时)==>onShow()==>onStart()==>onHide()==>onClose()
    /**只会在首次加载时调用一次，可以用于获取节点，绑定事件 */
    protected onInit() {
        super.onInit();
    }

    /**每次打开UI前，onStart前,每次呼出前赋值传参的时候用 */
    protected onOpen(){
        super.onOpen();
    }

    /**开始动画播完 onShow之后*/
    protected onStart(){
        super.onStart();
    }

    /**关闭动画结束后 */
    protected onClose(){
        super.onClose();
    }

    /**update生命周期 */
    protected onUpdate(dt) {
    }

    initView(){
        this.txtMsg.string = this.vc.msg;
        this.txtTitle.string = this.vc.title;
        this.initButton();
    }

    initButton() {
        this.btnClose.active = !this.vc.hideBtnClose;
        this.btnMiddle.active = this.vc.type === MsgBoxType.OK;
        this.btnLeft.active = this.vc.type === MsgBoxType.OK_CANCEL;
        this.btnRight.active = this.vc.type === MsgBoxType.OK_CANCEL;
        if (this.vc.type === MsgBoxType.OK) {
            this.btnMiddle.getComponentInChildren(cc.Label).string = this.vc.btnStrOk;
            this.setClickListener(this.btnMiddle, (ev: cc.EventTouch) => {
                this.vc.onClickOk(ev);
            },this);
        } else {
            this.btnLeft.getComponentInChildren(cc.Label).string = this.vc.btnStrOk;
            this.btnRight.getComponentInChildren(cc.Label).string = this.vc.btnStrCancel;
            this.setClickListener(this.btnLeft, (ev: cc.EventTouch)=>{
                this.vc.onClickOk(ev);
            },this);
            this.setClickListener(this.btnRight, (ev: cc.EventTouch)=>{
                this.vc.onClickCancel(ev);
            },this);
        }

        if (this.vc.bgClose) {
            this.setClickListenerNoAnim(this.background, (ev: cc.EventTouch) => {
                this.vc.onClickCancel(ev);
            }, this);
        }
    }

    /**通用关闭按钮响应事件,子类可重写 */
    protected onBtnClose_click(ev: cc.EventTouch) {
        this.vc.onClickCancel(ev)
    }
}